﻿using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;
namespace AnyOffice.general.meeting.apply
{
    public partial class edit : ModulePage
    {


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                int meetingId = (Request.QueryString["meeting_id"] != null) ? int.Parse(Request.QueryString["meeting_id"]) : 0;
                this.ViewState["MeetingId"] = meetingId;
                int num2 = (Request.QueryString["status"] != null) ? int.Parse(Request.QueryString["status"]) : 0;
                this.ViewState["Status"] = num2;
                MeetingDao dao = new MeetingDao();
                DataTable table = dao.SelectByMeetingId(base.Conn, meetingId);
                if (table.Rows.Count == 0)
                {
                    this.pnlMain.Visible = false;
                    this.litMessage.Text = base.ShowPrompt("无此会议！", "index.aspx");
                    return;
                }
                DataTable table2 = dao.SelectOperator(base.Conn);
                this.ddlOperator.DataSource = table2;
                this.ddlOperator.DataValueField = "UserId";
                this.ddlOperator.DataTextField = "UserName";
                this.ddlOperator.DataBind();
                DataTable table3 = new MeetingRoomDao().SelectAll(base.Conn);
                this.ddlRoomId.DataSource = table3;
                this.ddlRoomId.DataValueField = "RoomId";
                this.ddlRoomId.DataTextField = "Name";
                this.ddlRoomId.DataBind();
                if (table.Rows[0]["Proposer"].ToString() != base.User.Identity.Name)
                {
                    this.pnlMain.Visible = false;
                    this.litMessage.Text = base.ShowPrompt("无此会议！", "index.aspx");
                    return;
                }
                this.txtName.Value = (table.Rows[0]["Name"] != DBNull.Value) ? table.Rows[0]["Name"].ToString() : "";
                this.txtTopic.Value = (table.Rows[0]["Topic"] != DBNull.Value) ? table.Rows[0]["Topic"].ToString() : "";
                this.txtDescription.Value = (table.Rows[0]["Description"] != DBNull.Value) ? table.Rows[0]["Description"].ToString() : "";
                this.txtProposer.Value = (table.Rows[0]["ProposerName"] != DBNull.Value) ? table.Rows[0]["ProposerName"].ToString() : "";
                this.txtAttendee.Value = (table.Rows[0]["AttendeeName"] != DBNull.Value) ? table.Rows[0]["AttendeeName"].ToString() : "";
                this.Attendee.Value = (table.Rows[0]["Attendee"] != DBNull.Value) ? table.Rows[0]["Attendee"].ToString() : "";
                this.txtGuest.Value = (table.Rows[0]["Guest"] != DBNull.Value) ? table.Rows[0]["Guest"].ToString() : "";
                this.txtRequestTime.Value = (table.Rows[0]["RequestTime"] != DBNull.Value) ? table.Rows[0]["RequestTime"].ToString() : DateTime.Now.ToString();
                this.txtStartTime.Value = (table.Rows[0]["StartTime"] != DBNull.Value) ? table.Rows[0]["StartTime"].ToString() : DateTime.Now.ToString();
                this.txtEndTime.Value = (table.Rows[0]["EndTime"] != DBNull.Value) ? table.Rows[0]["EndTime"].ToString() : DateTime.Now.ToString();
                if (this.ddlRoomId.Items.FindByValue(table.Rows[0]["RoomId"].ToString()) != null)
                {
                    this.ddlRoomId.Items.FindByValue(table.Rows[0]["RoomId"].ToString()).Selected = true;
                }
                this.ckbSmsRemind.Checked = Convert.ToBoolean(table.Rows[0]["SmsRemind"].ToString());
                this.ckbMobileRemind.Checked = Convert.ToBoolean(table.Rows[0]["MobileRemind"].ToString());
                this.ckbMobile.Visible = SmsDao.IsMobile(SmsDao.SmsType.Meeting);
                this.ckbMobileRemind.Visible = SmsDao.IsMobile(SmsDao.SmsType.Meeting);
            }
            if (this.Page.IsPostBack)
            {
                this.pnlMain.Visible = false;
                MeetingDao dao3 = new MeetingDao();
                MeetingData data = new MeetingData();
                data.MeetingId = Convert.ToInt32(this.ViewState["MeetingId"]);
                data.Name = this.txtName.Value;
                if (this.txtTopic.Value != string.Empty)
                {
                    data.Topic = this.txtTopic.Value;
                }
                if (this.txtDescription.Value != string.Empty)
                {
                    data.Description = this.txtDescription.Value;
                }
                data.Proposer = base.User.Identity.Name;
                data.RequestTime = DateTime.Now;
                if (this.Attendee.Value != string.Empty)
                {
                    data.Attendee = this.Attendee.Value;
                }
                if (this.txtGuest.Value != string.Empty)
                {
                    data.Guest = this.txtGuest.Value;
                }
                data.StartTime = Convert.ToDateTime(this.txtStartTime.Value);
                data.EndTime = Convert.ToDateTime(this.txtEndTime.Value);
                data.RoomId = Convert.ToInt32(this.ddlRoomId.SelectedValue);
                data.SmsRemind = this.ckbSmsRemind.Checked;
                data.MobileRemind = this.ckbMobileRemind.Checked;
                dao3.Update(base.Conn, data);
                if ((this.ckbSms.Checked || this.ckbMobile.Checked) && (this.ddlOperator.SelectedValue != string.Empty))
                {
                    SmsDao dao4 = new SmsDao();
                    SmsData data2 = new SmsData();
                    data2.Type = 8;
                    data2.FromId = base.User.Identity.Name;
                    data2.ToId = this.ddlOperator.SelectedValue;
                    data2.Content = "您有新的会议申请需要审批。";
                    data2.SendTime = DateTime.Now;
                    dao4.BizSend(base.Conn, data2, this.ckbSms.Checked, this.ckbMobile.Checked);
                }
                this.litMessage.Text = base.ShowPrompt("编辑会议申请成功！", "query.aspx?status=" + this.ViewState["Status"].ToString());
            }
        }

    }
}
